package com.company.cloud.base.modules.app.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.base.modules.base.entity.ParameterInfo;
import com.company.cloud.base.modules.base.service.ParameterInfoService;
import com.company.cloud.base.modules.cache.models.ParameterModel;
import com.company.cloud.base.modules.cache.models.RegionModel;
import com.company.cloud.base.modules.cache.service.ParameterCacheService;
import com.company.cloud.base.modules.cache.utils.BaseConvertUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/base/parameter")
@Api(tags = "App_Base_业务参数信息")
public class ParameterAppController {

    @Autowired
    private ParameterCacheService parameterCacheService;

    @Autowired
    private ParameterInfoService parameterInfoService;

    @GetMapping("/get")
    @ApiOperation(value = "获取业务参数信息", response = String.class)
    public BaseResult queryAll(@RequestParam("key") String key) {
        if (StrUtil.isEmpty(key)) {
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        ParameterModel data = parameterCacheService.getParameterByKey2Cache(key);
        if (data != null) {
            return BaseResult.ok(data.getParaVal());
        }
        return BaseResult.ok();
    }

    @GetMapping("/getByType")
    @ApiOperation(value = "获取业务参数信息", response = String.class)
    public BaseResult getByType(@RequestParam("type") String type) {
        if (StrUtil.isEmpty(type)) {
            throw new BusinessException(BaseReturnEnum.PARAM_NULL);
        }
        List<ParameterInfo> list = parameterInfoService.list(new QueryWrapper<ParameterInfo>()
                .eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
                .eq("para_type", type)
                .orderByDesc("orders")
        );
        if (CollUtil.isEmpty(list)) {
            return BaseResult.ok();
        }

        List<ParameterModel> data = new ArrayList<>();
        list.forEach(item -> {
            data.add(BaseConvertUtils.getParameterModel(item));
        });
        return BaseResult.ok(data);
    }

}
